﻿using Carer.Crm.Utility;
using Carer.Crm.ViewModel;
using Carer.Data;
using Carer.Data.Enum;
using Carer.Data.Json;
using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace Carer.Crm.Controllers
{
    public abstract class BaseController : System.Web.Mvc.Controller
    {
        protected Unit GetUserUnit()
        {
            return base.Session["UserUnit"] as Unit;
        }
        protected int? GetUserUnitId()
        {
            Unit unit = base.Session["UserUnit"] as Unit;
            return ((unit == null || unit.Id == 0) ? null : new int?(unit.Id));
        }

        protected int? GetUserId()
        {
            return base.Session["UserId"] as int?;
        }
        protected EnumRole GetRole(int? role)
        {
            if (role.HasValue)
            {
                if (role.Value == 0)
                {
                    return EnumRole.Platform_Manager;
                }
                else if (role.Value == 1)
                {
                    return EnumRole.Unit_Manager;
                }
                else
                {
                    return EnumRole.None;
                }
            }

            return EnumRole.None;
        }
        protected IndexModel InitData()
        {
            var indexModel = new IndexModel()
            {
                AllUnits = DataService.GetAllUnits(),
                MenuAddr = new List<String>(),
                Role = EnumRole.None
            };

            if (Session != null && Session["UserInfo"] != null)
            {
                var userInfo = Session["UserInfo"] as User;
                indexModel.UserName = userInfo.Name;
                indexModel.Role = GetRole(userInfo.Role);
                if (userInfo.Unit != null)
                {
                    indexModel.UserUnit = userInfo.Unit;
                }
                indexModel.Menu = MenuHelper.GetMenu(indexModel.Role);
            }
            if (Session["UserUnit"] != null)
            {
                indexModel.UserUnit = Session["UserUnit"] as Unit;
            }

            return indexModel;
        }

        protected ViewResult ViewModel()
        {
            return ViewModel(null);
        }

        protected ViewResult ViewModel(IList<string> menuAddr)
        {
            var indexModel = InitData();
            if (menuAddr != null)
            {
                foreach (var item in menuAddr)
                {
                    indexModel.MenuAddr.Add(item);
                }
            }

            return View(indexModel);
        }
        /// <summary>
        /// 返回JsonResult
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="contentType">内容类型</param>
        /// <param name="contentEncoding">内容编码</param>
        /// <param name="behavior">行为</param>
        /// <returns>JsonReuslt</returns>
        protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new CustomJsonResult
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior,
                DateFormat = "yyyy-MM-dd"
            };
        }
        /// <summary>
        /// 返回JsonResult        
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="behavior">行为</param>
        /// <param name="dateFormat">日期数据格式</param>
        /// <returns>Json</returns>
        protected JsonResult Json(object data, JsonRequestBehavior behavior, string dateFormat)
        {
            return new CustomJsonResult
            {
                Data = data,
                JsonRequestBehavior = behavior,
                DateFormat = dateFormat
            };
        }

        /// <summary>
        /// 返回JsonResult        
        /// </summary>
        /// <param name="data">数据</param>
        /// <param name="dateFormat">日期数据格式</param>
        /// <returns>Json</returns>
        protected new JsonResult Json(object data, string dateFormat)
        {
            return new CustomJsonResult
            {
                Data = data,
                DateFormat = dateFormat
            };
        }
    }
}
